package com.atfcm.arr;

/**
 * @Author whaty
 * @create 2024/10/17 16:42
 */
public class t66 {
    public static void main(String[] args) {
        int[] data = {1,2,3};
        int[] ints = plusOne2(data);

        for (int anInt : ints) {
            System.out.println(anInt);
        }

    }

    /**
     * 答案1 long会溢出
     * @param digits
     * @return
     */
    public static int[] plusOne(int[] digits) {
        long num = 0;
        for (int i = 0; i < digits.length; i++) {
            long pow = (int) Math.pow(10.0d, Double.valueOf(Integer.valueOf(i)));
            num += digits[digits.length - 1 - i] * pow;
            System.out.println(digits[digits.length - 1 - i] + "*" + pow);
        }
        num++;

        long divNum = num;

        int count = 1;
        while (num != 0) {
            num /= 10;
            if (num != 0) {
                count++;
            }
        }
        int[] res = new int[count];

        for (int i = count - 1; i >= 0; i--) {
            long pow = (int) Math.pow(10.0d, Double.valueOf(Integer.valueOf(i)));
            int h = (int) (divNum / pow);
            res[count - 1 - i] = h;
            divNum = divNum - (h * pow);

        }
        return res;
    }

    /**
     * 答案2
     * 边界  9  99 999 会进位
     * @param digits
     * @return
     */
    public static int[] plusOne2(int[] digits) {
        if (digits[digits.length - 1] == 9) {
            for (int i = digits.length - 1; i >= 0; i--) {
                if (digits[i] != 9) {
                }
                int res = digits[i] + 1;
                if (res == 10) {
                    digits[i] = 0;
                } else {
                    digits[i] = res;
                    return digits;
                }
            }

            int[] newArr = new int[digits.length + 1];
            newArr[0] = 1;
            for (int i = 0; i < digits.length; i++) {
                newArr[i + 1] = digits[i];
            }
            return newArr;
        } else {
            digits[digits.length - 1] += 1;
            return digits;
        }
    }


}

